#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_set>
using namespace std;

class Solution{
    public:
    vector<int> top(int n){
        vector<int> each_bit;
        while(true){
            each_bit.push_back(n % 10);
            n = n / 10;
            if(n == 0){
                break;
            }
        }
        return each_bit;
    }

    int sum(int n){
        int ret = 0;
        while(true){
            ret = ret + (n % 10) * (n % 10);
            n = n / 10;
            if(n == 0){
                break;
            }
        }
        return ret;
    }

    //哈希表
    bool ans1(int n){
        unordered_set<int> map;
        int temp = n;
        while(true){
            // vector<int> ret = top(temp);
            // int judge = 0;
            // for(int i = 0; i != ret.size(); i++){
            //     judge = ret[i] * ret[i] + judge;
            // }
            int judge = sum(temp);
            if(judge == 1){
                return true;
            }
            auto it = map.find(judge);
            if(it != map.end()){
                return false;
            }
            map.insert(judge);
            temp = judge;
        }
    }

    
    
};

void test1(){
    Solution s;
    int target = 19;
    bool ret = s.ans1(target);
    cout << ret << endl;
}

void test2(){
    Solution s;
    int target = 2;
    bool ret = s.ans1(target);
    cout << ret << endl;
}


int main(void){
    test1();
    test2();


    return 0;
}